package cryptx

import (
	"codeup.aliyun.com/lsxd/zltx/adapter/utils/cryptx/cipher"
	"codeup.aliyun.com/lsxd/zltx/adapter/utils/cryptx/sm4"
	"codeup.aliyun.com/lsxd/zltx/adapter/utils/formater"
)

type sm4Builder struct {
	base *BaseCrypto
}

// SetFormat 设置数据格式
func (b *sm4Builder) SetFormat(f formater.Formater) *sm4Builder {
	b.base.SetFormat(f)
	return b
}

// SetMode 设置加密模式
func (b *sm4Builder) SetMode(m cipher.CipherMode) *sm4Builder {
	b.base.SetMode(m)
	return b
}

// SetKey 设置密钥
func (b *sm4Builder) SetKey(key string) *sm4Builder {
	b.base.SetKey(0, key)
	return b
}

// SetIV 设置初始向量
func (b *sm4Builder) SetIV(iv string) *sm4Builder {
	b.base.SetKey(1, iv)
	return b
}

// SetPadding 设置填充方式
func (b *sm4Builder) SetPadding(p cipher.CipherPadding) *sm4Builder {
	b.base.SetPadding(p)
	return b
}

// Build 构建加密器
func (b *sm4Builder) Build() Crypto {
	cp := b.base.MyCipher()
	d := sm4.NewSm4(
		sm4.WithFormat(b.base.fmt),
		sm4.WithCipher(cp),
	)
	return d
}
